define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form,undefined) {
    var Controller = {
        surplus: function () {
            // 初始化表格参数配置
            var table = $("#table");

            Table.api.init({
                extend: {
                    index_url: 'apply/apply/surplus',
                    add_url: '',
                    edit_url: 'apply/apply/edit',
                    // del_url: 'auth/adminlog/del',
                    multi_url: 'auth/adminlog/multi',
                    table:'apply',
                },
                sortName: 'create_time',

            });

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                search: false,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'create_time', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'qq', title: 'QQ'},
                        {field: 'weixin', title: '微信'},
                        {field: 'tel', title: '号码'},
                        {field: 'platform', title: '来源平台',formatter:Table.api.formatter.search},
                        {field: 'zhimafen', title: '芝麻分'},
                        {field: 'username', title: '姓名'},
                        {field: 'is_reg', title: '注册次数'},
                        {field: 'age', title: '年龄'},
                        {field: 'bignumber', title: '已卖的次数',sortable: true},
                        {field: 'neednumber', title: '需要出售次数',sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                    ]
                ]
            });

            // 为表格2绑定事件
            Table.api.bindevent(table);

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        score: function () {
            Controller.api.bindevent();
        },
        detail:function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/detail',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'group.name', visible: false,title: __('Group')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'),visible: false, operate: 'LIKE'},
                        {field: 'email', title: __('Email'), visible: false,operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'parent_id', title: '父级', operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'),visible: false, formatter: Table.api.formatter.image, operate: false},
                        {field: 'level', title: __('Level'), operate: 'BETWEEN', visible: false,sortable: true},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        // {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        // {field: 'into_the_number', title: '日进件数', sortable: true},
                        // {field: 'delay_show_time', title: '进件数延迟显示时间', sortable: true},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), visible: false,formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons:[
                                // {name:'add_score',title:'积分管理',icon:'fa fa-usd',classname: 'btn btn-xs btn-primary btn-dialog',url:'user/user/score'},
                                {
                                    name:'del_score',title:'删除',icon:'fa fa-times',classname: 'btn btn-xs btn-success btn-magic btn-ajax',url:'user/user/status',
                                    success: function (data, ret) {
                                        if ( ret.code == 1){
                                            $("#table").bootstrapTable('refresh');
                                        }else{

                                        }
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            // Controller.api.bindevent();
            Table.api.bindevent(table);
        },
        order:function(){
            Controller.api.bindevent();
        },
        bet:function () {


            _init = {
                index_url: 'user/user/index',
                add_url: 'user/user/add',
                edit_url: 'user/user/edit',
                // del_url: 'user/user/del',
                multi_url: 'user/user/multi',
                table: 'user'
            };

            // 初始化表格参数配置
            Table.api.init({
                extend: _init
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true,operate: false,formatter: Controller.api.formatter.gametype},
                        {field: 'group.name', visible: false,title: __('Group')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'),visible: false, operate: 'LIKE'},
                        {field: 'email', title: __('Email'), visible: false,operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'parent_id', title: '父级', operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'),visible: false, formatter: Table.api.formatter.image, operate: false},
                        {field: 'level', title: __('Level'), operate: 'BETWEEN', visible: false,sortable: true},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        {field: 'into_the_number', title: '日进件数', sortable: true},
                        {field: 'delay_show_time', title: '进件数延迟显示时间', sortable: true},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), visible: false,formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons:[
                                {name:'add_score',title:'进件',icon:'fa fa-heart',classname: 'btn btn-xs btn-primary btn-dialog',url:'user/user/order'},
                                {name:'add_score',title:'积分管理',icon:'fa fa-usd',classname: 'btn btn-xs btn-primary btn-dialog',url:'user/user/score'},
                                {
                                    name:'del_score',title:'删除',icon:'fa fa-times',classname: 'btn btn-xs btn-success btn-magic btn-ajax',url:'user/user/status',
                                    success: function (data, ret) {
                                        if ( ret.code == 1){
                                            $("#table").bootstrapTable('refresh');
                                        }else{

                                        }
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            // Controller.api.bindevent();
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                gametype: function (value, row, index) {
                    //这里手动构造URL
                    url = "user/user/detail?parent_id" + "=" + value;
                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    return '<a href="' + url + '" class="label label-success addtabsit" >' + value + '</a>';
                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="user/user/multi" data-id="' + row.id + '" data-params="status=hidden"><i class="fa ' + (row.title == '' ? 'fa-toggle-off' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                }
            }


        }
    };
    return Controller;


});